package com.zdlee.netty.server4;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @author zdlee
 * @description: ${DESCRIPTION}
 * @create 2018-11-27 14:54
 */
public class NettyClient {

    private static final int MAX_RETRY = 5;


    public static void main(String[] args) {

        NioEventLoopGroup workerGroup=new NioEventLoopGroup();
        Bootstrap bootstrap=new Bootstrap();
        bootstrap
                // 1.指定线程模型
                .group(workerGroup)
                // 2.指定 IO 类型为 NIO
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel socketChannel) throws Exception {

                    }
                });
        //建立连接
       /* bootstrap.connect("juejin.im",80).addListener(future->{
            if(future.isSuccess())
            {
                System.out.println("连接成功!");
            }else
            {
                System.err.println("连接失败!");
            }
        });*/
        connect(bootstrap,"juejin.im",80,MAX_RETRY);

    }

    private static  void connect(Bootstrap bootstrap,String host,int port,int retry){
        bootstrap.connect(host,port).addListener(future -> {
           if(future.isSuccess())
           {
               System.out.println("连接成功");
           }else if(retry==0)
           {
               System.err.println("重试次数已用完，放弃连接！");
           }else
           {
               // 第几次重连
               int order = (MAX_RETRY - retry) + 1;
               int delay=1 << order;
               System.err.println(new Date() + ": 连接失败，第" + order + "次重连……");
               bootstrap.config().group().schedule(()->connect(bootstrap,host,port,retry-1),delay, TimeUnit.SECONDS);
           }
        });
    }

}
